package vip.meeet.kafka;

import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.PartitionInfo;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ListTopic {

    private static final Properties properties = new Properties();

    static {
        properties.put("bootstrap.servers", "127.0.0.1:9092,127.0.0.1:9093,127.0.0.1:9094");
        properties.put("group.id", "xx");
        properties.put("enable.auto.commit", "true");
        properties.put("auto.commit.interval.ms", "1000");
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    }

    public static void main(String[] args) {
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
        Duration duration = Duration.ofSeconds(5);
        Map<String, List<PartitionInfo>> stringListMap = consumer.listTopics(duration);
        Set<Map.Entry<String, List<PartitionInfo>>> entries = stringListMap.entrySet();
        for(Map.Entry<String, List<PartitionInfo>> entry : entries){
            System.out.println("topic:" + entry.getKey());
            List<PartitionInfo> infos = entry.getValue();
            infos.forEach(System.out::println);
        }
    }
}
